﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Baimasoft.Manage.SqlServerDAL;
using Baimasoft.FrameWork.Base;
using HsTec.Mobile.Green.SqlServerDAL;

public partial class BaseData_AreaManage : Baimasoft.Web.UI.PageManagerTop
{
    protected int count = 0;
    protected int j = 0;
    protected int i = 0;
    protected int t = 0;
    protected int m = 0;
    protected int n = 0;
    protected int ncount = 0;
    protected DataTable mylistTb = new DataTable();
    protected DataTable countlistTb = new DataTable();
    protected DataTable childCountListTb = new DataTable();
    protected DataTable childIDlistTb = new DataTable();
    protected DataTable childChildListTB = new DataTable();
    protected string mode = string.Empty;
    protected int PID = 0;
    protected string sqlString = string.Empty;

    Area MyArea = new Area();


    protected void Page_Load(object sender, EventArgs e)
    {
        this.BindControlEvent();

        DataTable dt = MyArea .GetAreaInfos ("ParentID=1");
        if (!this.Page.IsPostBack)
        {
            //try
            //{
            //    Indicator.Text = Request.QueryString["sIndicator"].ToString();
            //}
            //catch
            //{
            //}
            this.RepFather.DataSource = dt;
            this.RepFather.DataBind();
        }     

        mode=Request.QueryString["mode"]==null ? "" : Request.QueryString["mode"].ToString();
        PID=Request.QueryString["DelID"]==null ? -1 : int.Parse(Request.QueryString["DelID"].ToString());
        sqlString = DelString(mode, PID);
        if (sqlString.Equals(""))
        {
            //
        }
        else
        {
            SqlHelper.ExecuteNonQuery(AppConfig.ConnectionString, CommandType.Text, sqlString, null);
            Response.Redirect("AreaManage.aspx");
        }
        mylistTb.Columns.Add("ParentID");

        countlistTb.Columns.Add("childcount");

        childCountListTb.Columns.Add("countt");

        childIDlistTb.Columns.Add("childID");
        childIDlistTb.Columns.Add("ParentID");
        childIDlistTb.Columns.Add("AreaID");
        childIDlistTb.Columns.Add("Name");

        childChildListTB.Columns.Add("ID");
        childChildListTB.Columns.Add("ParentID");
        childChildListTB.Columns.Add("AreaID");
        childChildListTB.Columns.Add("CityName");

        for (int i1 = 0; i1 < dt.Rows.Count; i1++)   //取表中的一级目录
        {
            DataRow mylistTbRow = mylistTb.NewRow();
            mylistTbRow["ParentID"] = int.Parse(dt.Rows[i1]["ID"].ToString());
            mylistTb.Rows.Add(mylistTbRow);
        }

        for (int j = 0; j < mylistTb.Rows.Count; j++)  //取二级目录
        {
            string sql = string.Format(@"select count(*) from Area where ParentID={0}", int.Parse(mylistTb.Rows[j]["ParentID"].ToString()));
            DataRow countlistRow = countlistTb.NewRow();
            countlistRow["childcount"] = (System.Convert.ToInt32(SqlHelper.ExecuteScalar(AppConfig.ConnectionString, CommandType.Text, sql, null)));
            countlistTb.Rows.Add(countlistRow);
            string sql2 = string.Format(@"select ID,AreaID,Name from Area where ParentID={0} order by AreaID asc", int.Parse(mylistTb.Rows[j]["ParentID"].ToString()));
            SqlDataReader dr = SqlHelper.ExecuteReader(AppConfig.ConnectionString, CommandType.Text, sql2, null);
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    DataRow childIDlistRow = childIDlistTb.NewRow();
                    childIDlistRow["childID"] = int.Parse(dr["ID"].ToString());
                    childIDlistRow["ParentID"] = int.Parse(mylistTb.Rows[j]["ParentID"].ToString());
                    childIDlistRow["AreaID"] = int.Parse(dr["AreaID"].ToString());
                    childIDlistRow["Name"] = dr["Name"].ToString();
                    childIDlistTb.Rows.Add(childIDlistRow);
                }
            }
            dr.Close();
            dr.Dispose();
        }

        for (int k = 0; k < childIDlistTb.Rows.Count; k++) //取三级目录
        {
            string sqll = string.Format(@"select count(*) from Area where ParentID={0}", int.Parse(childIDlistTb.Rows[k]["childID"].ToString()));
            DataRow counttRow = childCountListTb.NewRow();
            counttRow["countt"] = (System.Convert.ToInt32(SqlHelper.ExecuteScalar(AppConfig.ConnectionString, CommandType.Text, sqll, null)));
            childCountListTb.Rows.Add(counttRow);
            string childSqlString = string.Format(@"select ID,AreaID,Name from Area where ParentID={0} order by AreaID asc", int.Parse(childIDlistTb.Rows[k]["childID"].ToString()));
            SqlDataReader dr1 = SqlHelper.ExecuteReader(AppConfig.ConnectionString, CommandType.Text, childSqlString, null);
            if (dr1.HasRows)
            {
                while (dr1.Read())
                {
                    DataRow childChildRow = childChildListTB.NewRow();
                    childChildRow["ID"] = int.Parse(dr1["ID"].ToString());
                    childChildRow["ParentID"] = int.Parse(childIDlistTb.Rows[k]["ParentID"].ToString());
                    childChildRow["AreaID"] = int.Parse(dr1["AreaID"].ToString());
                    childChildRow["CityName"] = dr1["Name"].ToString();
                    childChildListTB.Rows.Add(childChildRow);
                }
            }
            dr1.Close();
            dr1.Dispose();
        }
    }

    protected string GetChileName(int childid)
    {
        DataTable dt = MyArea .GetAreaInfos ("ID=" + childid + "");
        return dt.Rows[0]["Name"].ToString();
    }

    protected string GetStringOrder(string Str)
    {
        int StrLen = Str.Length;
        string strValue=string.Empty;
        switch (StrLen)
        {
            case 1: strValue = "0" + Str;
                break;
            case 2: strValue = Str;
                break;
            default: strValue = Str;
                break;
        }
        return strValue;
    }
    protected string GetChildStringOrder(string Str)
    {
        int StrLen = Str.Length;
        string strValue = string.Empty;
        switch (StrLen)
        {
            case 1: strValue = "00" + Str;
                break;
            case 2: strValue = "0"+Str;
                break;
            default: strValue = Str;
                break;
        }
        return strValue;
    }
    protected string  DelString(string mode, int delid)
    {
        string delsql = ""; 
        if (mode == "pDel")
        {
            delsql = string.Format(@"delete from Area where ID={0} or ParentID={1}", delid,delid);
        }
        if (mode == "cDel")
        {
            delsql = string.Format(@"delete from Area where ID={0} or ParentID={1}", delid,delid);
        }
        if (mode == "ccDel")
        {
            delsql = string.Format(@"delete from Area where ID={0}",delid);
        }
        return delsql;

    }

}
